package gx.web.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import gx.entity.Customer;
import gx.entity.common.Page;
import gx.service.CustomerService;

@Controller
@RequestMapping("/mng/cust")
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	
	//查询列表
	@RequestMapping("/list.jhtml")
	public ModelAndView list(@RequestParam(name="size",defaultValue="10")int size,
			@RequestParam(name="number",defaultValue="1")int number){
		ModelAndView mav = new ModelAndView("mng/cust/list");
		Page<Customer> page =customerService.findByPager(size, number);
		mav.addObject("pager",page);
		return mav;
	}
	//todo
	//添加
	@RequestMapping("/add.jhtml")
	public String add(Customer customer){
		customer.setSn(UUID.randomUUID().toString());
		customerService.save(customer);
		return "redirect:/mng/cust/list.jhtml";
	}
	
	//编辑--获取原数据
	@RequestMapping("/detail.jhtml")
	public ModelAndView detail(Integer id){
		ModelAndView mav = new ModelAndView("mng/cust/detail");
		Customer customer = customerService.get(id);
		mav.addObject("cust", customer);
		return mav;
	}
	
	//修改
	@RequestMapping("/edit.jhtml")
	public String edit(String lname,String address,String email,String code,String contact,String remark,String tel,Integer id){
		Customer cust = customerService.get(id);
		cust.setlname(lname);
		cust.setAddress(address);
		cust.setEmail(email);
		cust.setCode(code);
		cust.setContact(contact);
		cust.setRemark(remark);
		cust.setTel(tel);
		customerService.update(cust);
		return "redirect:/mng/cust/list.jhtml";
	}
	
	//删除
	@RequestMapping("/delete.jhtml")
	public String delete(Integer id){
		customerService.delete(id);
		return "redirect:/mng/cust/list.jhtml";
	}
	
	//按客户名称查询
	@RequestMapping("/findByName.jhtml")
	public ModelAndView findByName(@RequestParam(name="size",defaultValue="10")int size,
			@RequestParam(name="number",defaultValue="1")int number,String condition){
		ModelAndView mav = new ModelAndView("/mng/cust/list");
		Page<Customer> page = customerService.findByName(size,number,condition);
        mav.addObject("pager",page);
		return mav ;
	}
	
	@RequestMapping(value = "/ajaxdelete.jhtml", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String ajaxList(Integer id) throws Exception {
		String rs = "";
		try {
			customerService.delete(id);
			rs = "删除成功！";
		} catch (Exception e) {
			rs = "该客户已制单，无法删除！";
		}
		// 使用Jackson包来处理json
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(rs);
		return json;
	}
	
}
